package com.dykj.fireFighting.web.base.websocket.listener;

import com.dykj.fireFighting.api.base.fireEngine.vo.FireEngineMessage;
import com.dykj.fireFighting.common.core.utils.JacksonUtil;
import com.dykj.fireFighting.web.base.websocket.CommonWebSocket;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

/**
 * @description: redis监听类
 * @author: hxb
 * @create: 2021-11-23 15:58
 */
@Slf4j
@Component
public class RedisListener implements MessageListener {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private CommonWebSocket commonWebSocket;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        log.info("消防车集群监听websocket消息--- message:{}", message);
        RedisSerializer<String> valueSerializer = redisTemplate.getStringSerializer();
        String value = valueSerializer.deserialize(message.getBody());
        if (StringUtils.isNotBlank(value)) {
            log.info("消防车监听集群websocket消息--- value:{}", value);
            FireEngineMessage fireEngineMessage = JacksonUtil.toObject(value, FireEngineMessage.class);
            //推送消息
            commonWebSocket.sendOneMessage(fireEngineMessage.getUserId(), value);
        }
    }
}
